#!/bin/bash

#变量
software_dir="llama3"

# 安装必要软件
sudo snap install nvtop
sudo apt-get install git-lfs
git lfs install

#下载文件
[ ! -d "$software_dir" ] && git clone https://github.com/meta-llama/llama3.git $software_dir
cd $software_dir
[ ! -d "amdllama3" ] && python3 -m venv amdllama3
source amdllama3/bin/activate

# 安装依赖
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/rocm6.0
pip3 install --pre torchtune --extra-index-url https://download.pytorch.org/whl/nightly/rocm --force-reinstall --no-cache-dir
pip3 uninstall numpy -y
pip3 install numpy>=1.22.4
pip uninstall wandb
pip3 install wandb

# 下载模型
[ ! -d "Llama3-8B-Chinese-Chat" ] && git clone https://hf-mirror.com/shenzhi-wang/Llama3-8B-Chinese-Chat.git

# 克隆配置库
cd ..
[ ! -d "torchtune-amd-recipes" ] && git clone https://github.com/farshadghodsian/torchtune-amd-recipes.git
cd torchtune-amd-recipes
sed -i 's|\$USER|'"${USER}"'|g' recipes/configs/llama3/*.yaml
sed -i 's@/home/amd/Documents/llama3/Meta-Llama-3-8B-Instruct@/home/amd/llama3/Llama3-8B-Chinese-Chat@g' recipes/configs/llama3/8B_lora_single_device.yaml
sed -i 's@/home/amd/Documents/torchtune-amd-recipes/output@/home/amd/llama3/output@g' recipes/configs/llama3/8B_lora_single_device.yaml
sed -i 's@dtype: bf16@dtype: fp32@g' recipes/configs/llama3/8B_lora_single_device.yaml

# 启动 wandb 并自动登录
export WANDB_API_KEY="5e3b090215f3342fac93e06cb710d26d3cc66131"
wandb login --relogin

# 模型微调
tune run lora_finetune_single_device --config ./recipes/configs/llama3/8B_lora_single_device.yaml
